package com.agrm.user.service.impl;

import com.agrm.model.common.dtos.PageRequestDto;
import com.agrm.model.user.dto.UserQuery;
import com.agrm.model.user.pojo.User;
import com.agrm.user.mapper.UserMapper;
import com.agrm.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询
     * @param page  分页参数
     * @param query 查询参数
     * @return
     */
    @Override
    public IPage<User> pageQueryList(PageRequestDto page, UserQuery query) {
        page.checkParam();
        // 创建MyBatis-Plus分页对象
        Page<User> pages = new Page<>(page.getPage(), page.getSize());
        // 构建查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (query != null){
            // 用户名 模糊查询
            if (query.getUsername() != null && !query.getUsername().isEmpty()) {
                wrapper.like(User::getUsername, query.getUsername());
            }
            //启用
            if (query.getEnabled() != null) {
                wrapper.eq(User::getEnabled, query.getEnabled());
            }
            // 创建时间范围查询
            if (query.getCreateStartTime() != null && query.getCreateEndTime() != null) {
                wrapper.between(User::getCreatedTime, query.getCreateStartTime(), query.getCreateEndTime());
            }
        }
        return userMapper.selectPage(pages, wrapper);
    }
}
